<?php
if (!defined("_ECRIRE_INC_VERSION")) return;
require_once(_DIR_PLUGIN_FEEDBACK.'classes/FeedbackEmail.php');
require_once(_DIR_PLUGIN_FEEDBACK.'classes/FeedbackFormHandler.php');
require_once(_DIR_PLUGIN_PDK.'classes/phpmailer/class.phpmailer.php');
require_once(_DIR_PLUGIN_PDK.'classes/pdk_validation.php');

include_spip('formulaires/feedback2col/config');

/**
 *  Charging function
 */ 
function formulaires_feedback2col_charger_dist(){
    $valeurs = array();
    $valeurs['config'] = $GLOBALS['config_formulaire_feedback2col'];
    
    $valeurs['name'] = '';
    $valeurs['email'] = '';
    $valeurs['phone'] = '';
    $valeurs['text'] = '';
    
	return $valeurs;
}

/**
 *  Validation function
 */
function formulaires_feedback2col_verifier_dist(){    
    $config = $GLOBALS['config_formulaire_feedback2col'];
    $form_name = $config['form_name'];
	$erreurs = array();
    
    $validation = new pdk_Validation();
    $validation->set_rules( 
            array(
               array(
                     'field'   => 'name',
                     'label'   => '"'._T("formulaire_$form_name:name").'"',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'email',
                     'label'   => '"'._T("formulaire_$form_name:email").'"',
                     'rules'   => 'required|valid_email'
                  ),
               array(
                     'field'   => 'text',
                     'label'   => '"'._T("formulaire_$form_name:text").'"',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'phone',
                     'label'   => '"'._T("formulaire_$form_name:phone").'"',
                     'rules'   => 'required|numeric'
                  )                     
            )
         );
         
    $result = $validation->Validate($_REQUEST, $erreurs);
    
    // Check captcha
    if ($config['add_captcha']) {
        include_spip("securimage/securimage");    
        $img = new Securimage();    
        $img->namespace = $form_name;
        if (!$img->check(_request('captcha'))) {
            $erreurs['captcha'] = _T('securimage:wrong_code');
        }
    }
    
    // Upload files
    if ($result) {
        $form_handler = new FeedbackFormHandler($form_name);
        $form_handler->UploadFiles($file_error);
        if ($file_error) {
            $erreurs['file'] = $file_error;
            $result = false;
        }
    }
     
    if (!$result) {
        $erreurs['message_erreur'] = _T("formulaire_$form_name:form_error");
        include_spip('functions/pdk_message');
        pdk_set_message_error($erreurs['message_erreur'], $config['form_name']);
    }
        
    
	return $erreurs;
}

/**
 *  Processing function
 */
function formulaires_feedback2col_traiter_dist(){    
    $config = $GLOBALS['config_formulaire_feedback2col'];
    $form_name = $config['form_name'];
    include_spip('functions/pdk_message');
    
    if (!_request('url')) {
        $femail = new FeedbackEmail($form_name, $config);
        $return = array();    
        if ($femail->sendMessages()) {
            $return['message_ok'] = _T("formulaire_$form_name:message_ok");
            pdk_set_message_ok($return['message_ok'], $config['form_name']);
        } else {
            $return['message_erreur'] = _T("formulaire_$form_name:message_erreur");
            pdk_set_message_error($return['message_erreur'], $config['form_name']);
        }     
    } 
    
    // Redirect    
    if ($config['redirect_after_processing'] && $config['redirect_url']) {
        include_spip('inc/utils');
        $return['redirect'] = $config['redirect_url'];
    } 
    
    // Clear fields
    if ($config['clear_after_processing']) {
        foreach ($config['clear_fields'] as $field) {
            set_request($field);
        }    
    }
    
    return $return;    
}
?>